package fr.mairie.parc.page.user;

import javax.annotation.Resource;

import org.apache.click.control.Form;
import org.apache.click.control.ImageSubmit;
import org.apache.click.util.Bindable;
import org.springframework.context.ApplicationContext;

import fr.mairie.parc.context.AppContext;
import fr.mairie.parc.control.user.UserFields;
import fr.mairie.parc.domain.User;
import fr.mairie.parc.page.BorderPage;
import fr.mairie.parc.service.UserManager;

public class EditUserPage extends BorderPage{

	/**
	 *
	 */
	private static final long serialVersionUID = 1L;

	@Resource protected UserManager userManager;
	@Bindable protected Form form = new Form();
	@Bindable protected Integer idUser;
	protected transient User user;


	protected UserFields fields = new UserFields();


	public EditUserPage() {
		fields.addAll(form);

		form.add(new ImageSubmit("OK", "/assets/images/32x32/accept.png", this,"onSubmit"));
		form.add(new ImageSubmit("Cancel", "/assets/images/32x32/cancel.png",this, "onCancelClicked"));
	}

	@Override
	public void onInit() {
		super.onInit();
		ApplicationContext ctx = AppContext.getApplicationContext();
    	userManager = (UserManager) ctx.getBean("userManager");
	}

	@Override
	public void onRender() {
		if (idUser < 0) {
			msg = "Error, invalid id:" + idUser + " for User";
			setRedirect("error.htm");
			return;
		}
		if (user == null) {
			user =  userManager.getUserById(idUser);
		}
		if (user == null) {
			msg = "Error, Commande not found for id:" + idUser;
			setRedirect("error.htm");
			return;
		}
		fields.setFieldsValues(user);
		super.onRender();
	}

	public boolean onCancelClicked() {
        setRedirect(ListUserPage.class);
        return false;
    }

	public boolean onSubmit() {

		return true;
	}

}
